home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd1.bin / sharewar / FFE / GRAPH.SWG / 0046_JFIF and JPEG.pas < prev    next >
Pascal/Delphi Source File  |  1997-05-11  |  15KB  |  358 lines

  1. JPEG File Interchange Format
  2. Version 1.02
  3.  
  4.  
  5. September 1, 1992
  6.                             Eric Hamilton
  7.                             C-Cube Microsystems
  8.                             1778 McCarthy Blvd.
  9.                             Milpitas, CA 95035
  10.  
  11.                             +1 408 944-6300
  12.                             Fax: +1 408 944-6314
  13.                             E-mail: eric@c-cube.com
  14.  
  15.  
  16.  
  17.  
  18. JPEG File Interchange Format
  19. Version 1.02
  20.  
  21. Why a File Interchange Format
  22.  
  23. JPEG File Interchange Format is a minimal file format which enables JPEG
  24. bitstreams to be exchanged between a wide variety of platforms and
  25. applications.  This minimal format does not include any of the advanced
  26. features found in the TIFF JPEG specification or any application specific
  27. file format. Nor should it, for the only purpose of this simplified
  28. format is to allow the exchange of JPEG compressed images.
  29.  
  30. JPEG File Interchange Format features
  31.  
  32. o      Uses JPEG compression
  33. o      Uses JPEG interchange format compressed image representation
  34. o      PC or Mac or Unix workstation compatible
  35. o      Standard color space: one or three components. For three components,
  36.        YCbCr (CCIR 601-256 levels)
  37. o      APP0 marker used to specify Units, X pixel density, Y pixel density,
  38.        thumbnail
  39. o      APP0 marker also used to specify JFIF extensions
  40. o      APP0 marker also used to specify application-specific information
  41.  
  42. JPEG Compression
  43.  
  44. Although any JPEG process is supported by the syntax of the JPEG File 
  45. Interchange Format (JFIF) it is strongly recommended that the JPEG baseline 
  46. process be used for the purposes of file interchange. This ensures maximum 
  47. compatibility with all applications supporting JPEG. JFIF conforms to the 
  48. JPEG Draft International Standard (ISO DIS 10918-1).
  49.  
  50. The JPEG File Interchange Format is entirely compatible with the standard 
  51. JPEG interchange format;  the only additional requirement is the mandatory 
  52. presence of the APP0 marker right after the SOI marker.  Note that JPEG 
  53. interchange format requires (as does JFIF) that all table specifications
  54. used in the encoding process be coded in the bitstream prior to their use.
  55.  
  56. Compatible across platforms
  57.  
  58. The JPEG File Interchange Format is compatible across platforms: for 
  59. example, it does not use any resource forks, supported by the Macintosh but 
  60. not by PCs or workstations.
  61.  
  62.  
  63. Standard color space
  64.  
  65. The color space to be used is YCbCr as defined by CCIR 601 (256 levels).
  66. The RGB components calculated by linear conversion from YCbCr shall not be 
  67. gamma corrected (gamma = 1.0).  If only one component is used, that 
  68. component shall be Y.
  69.  
  70. APP0 marker used to identify JPEG FIF
  71.  
  72. The APP0 marker is used to identify a JPEG FIF file.  The JPEG FIF APP0 
  73. marker is mandatory right after the SOI marker.
  74.  
  75. The JFIF APP0 marker is identified by a zero terminated string: "JFIF". 
  76. The APP0 can be used for any other purpose by the application provided it 
  77. can be distinguished from the JFIF APP0.
  78.  
  79. The JFIF APP0 marker provides information which is missing from the JPEG 
  80. stream: version number, X and Y pixel density (dots per inch or dots per 
  81. cm), pixel aspect ratio (derived from X and Y pixel density), thumbnail.
  82.  
  83. APP0 marker used to specify JFIF extensions
  84.  
  85. Additional APP0 marker segment(s) can optionally be used to specify JFIF 
  86. extensions.  If used, these segment(s) must immediately follow the JFIF 
  87. APP0 marker.  Decoders should skip any unsupported JFIF extension 
  88. segments and continue decoding.
  89.  
  90. The JFIF extension APP0 marker is identified by a zero terminated string:  
  91. "JFXX".  The JFIF extension APP0 marker segment contains a 1-byte code 
  92. which identifies the extension.  This version, version 1.02, has only 
  93. one extension defined:  an extension for defining thumbnails stored in 
  94. formats other than 24-bit RGB.
  95.  
  96. APP0 marker used for application-specific information
  97.  
  98. Additional APP0 marker segments can be used to hold application-specific 
  99. information which does not affect the decodability or displayability of 
  100. the JFIF file.  Application-specific APP0 marker segments must appear 
  101. after the JFIF APP0 and any JFXX APP0 segments.  Decoders should skip any 
  102. unrecognized application-specific APP0 segments. 
  103.  
  104. Application-specific APP0 marker segments are identified by a zero 
  105. terminated string which identifies the application (not "JFIF" or "JFXX").  
  106. This string should be an organization name or company trademark.  Generic 
  107. strings such as dog, cat, tree, etc. should not be used. 
  108.  
  109.  
  110.  
  111. Conversion to and from RGB
  112.     
  113. Y, Cb, and Cr are converted from R, G, and B as defined in CCIR 
  114. Recommendation 601 but are normalized so as to occupy the full 256 levels 
  115. of a 8-bit binary encoding.  More precisely:
  116.  
  117. Y   = 256 * E'y
  118. Cb  = 256 * [ E'Cb ] + 128
  119. Cr  = 256 * [ E'Cr ] + 128
  120.  
  121. where the E'y, E'Cb and E'Cb are defined as in CCIR 601.  Since values of 
  122. E'y have a range of 0 to 1.0 and those for  E'Cb and E'Cr have a range of 
  123. -0.5 to +0.5,  Y, Cb, and Cr must be clamped to 255 when they are maximum 
  124. value.
  125.  
  126. RGB to YCbCr Conversion
  127.     
  128. YCbCr (256 levels) can be computed directly from 8-bit RGB as follows:
  129.  
  130. Y   =     0.299  R + 0.587  G + 0.114  B
  131. Cb  =   - 0.1687 R - 0.3313 G + 0.5    B + 128
  132. Cr  =     0.5    R - 0.4187 G - 0.0813 B + 128
  133.  
  134. NOTE - Not all image file formats store image samples in the order R0, G0, 
  135. B0, ... Rn, Gn, Bn.  Be sure to verify the sample order before converting an 
  136. RGB file to JFIF.
  137.  
  138.     
  139. YCbCr to RGB Conversion
  140.     
  141. RGB can be computed directly from YCbCr (256 levels) as follows:
  142.  
  143. R = Y                    + 1.402   (Cr-128)
  144. G = Y - 0.34414 (Cb-128) - 0.71414 (Cr-128)
  145. B = Y + 1.772   (Cb-128)
  146.  
  147.  
  148. Image Orientation
  149.  
  150. In JFIF files, the image orientation is always top-down.  This means that 
  151. the first image samples encoded in a JFIF file are located in the upper left 
  152. hand corner of the image and encoding proceeds from left to right and top to 
  153. bottom.  Top-down orientation is used for both the full resolution image 
  154. and the thumbnail image.
  155.  
  156. The process of converting an image file having bottom-up orientation to 
  157. JFIF must include inverting the order of all image lines before JPEG encoding.
  158.  
  159.  
  160. Spatial Relationship of Components
  161.  
  162. Specification of the spatial positioning of pixel samples within components 
  163. relative to the samples of other components is necessary for proper image 
  164. post processing and accurate image presentation.  In JFIF files, the 
  165. position of the pixels in subsampled components are defined with respect 
  166. to the highest resolution component.  Since components must be 
  167. sampled orthogonally (along rows and columns), the spatial position of the 
  168. samples in a given subsampled component may be determined by specifying the 
  169. horizontal and vertical offsets of the first sample, i.e. the sample in 
  170. the upper left corner, with respect to the highest resolution component.
  171.  
  172. The horizontal and vertical offsets of the first sample in a subsampled 
  173. component, Xoffseti[0,0] and Yoffseti[0,0], is defined to be
  174.  
  175. Xoffseti[0,0] = ( Nsamplesref / Nsamplesi  ) / 2 - 0.5
  176. Yoffseti[0,0] = ( Nlinesref / Nlinesi  ) / 2 - 0.5
  177.  
  178. where
  179.  
  180. Nsamplesref is the number of samples per line in the largest component,
  181. Nsamplesi is the number of samples per line in the ith component,
  182. Nlinesref is the number of lines in the largest component,
  183. Nlinesi is the number of lines in the ith component.
  184.   
  185. Proper subsampling of components incorporates an anti-aliasing filter 
  186. which reduces the spectral bandwidth of the full resolution components.   
  187. Subsampling can easily be accomplished using a symmetrical digital filter 
  188. with an even number of taps (coefficients).  A commonly used filter for 
  189. 2:1 subsampling utilizes two taps (1/2,1/2).
  190.  
  191. ED. NOTE:  Figures are not available in this plain text version of
  192. the specification.  See the PostScript version for a figure in this
  193. section.
  194.  
  195. NOTE - This definition is compatible with industry standards such as 
  196. Postcript Level 2 and QuickTime. This defintition is not compatible with the 
  197. conventions used by CCIR Recommendation 601-1 and other digital video 
  198. formats.  For these formats, pre-processing of the chrominance components 
  199. is necessary prior to compression in order to ensure accurate reconstruction 
  200. of the compressed image.
  201.  
  202.  
  203. JPEG File Interchange Format Specification
  204.  
  205. The syntax of a JFIF file conforms to the syntax for interchange format 
  206. defined in Annex B of ISO DIS 10918-1.  In addition, a JFIF file uses APP0 
  207. marker segments and constrains certain parameters in the frame header as 
  208. defined below.
  209.  
  210.     X'FF', SOI
  211.          X'FF', APP0, length, identifier, version, units, Xdensity, Ydensity, 
  212.          Xthumbnail, Ythumbnail, (RGB)n
  213.  
  214.               length     (2 bytes)  Total APP0 field byte count, including 
  215.                     the byte count value (2 bytes), but 
  216.                     excluding the APP0 marker itself
  217.               identifier (5 bytes)  = X'4A', X'46', X'49', X'46', X'00' 
  218.                                     This zero terminated string ("JFIF") 
  219.                     uniquely identifies this APP0 marker.  
  220.                         This string shall have zero parity 
  221.                     (bit 7=0).
  222.               version    (2 bytes)  = X'0102'
  223.                                     The most significant byte is used for 
  224.                     major revisions, the least significant 
  225.                     byte for minor revisions. Version 1.02 
  226.                     is the current released revision.
  227.               units      (1 byte)   Units for the X and Y densities.
  228.                                     units = 0:  no units, X and Y specify the 
  229.                         pixel aspect ratio
  230.                                     units = 1:  X and Y are dots per inch
  231.                                     units = 2:  X and Y are dots per cm
  232.               Xdensity   (2 bytes)  Horizontal pixel density
  233.               Ydensity   (2 bytes)  Vertical pixel density
  234.               Xthumbnail (1 byte)   Thumbnail horizontal pixel count
  235.               Ythumbnail (1 byte)   Thumbnail vertical pixel count
  236.               (RGB)n     (3n bytes) Packed (24-bit) RGB values for the 
  237.                     thumbnail pixels, n = Xthumbnail * 
  238.                     Ythumbnail
  239.         [ Optional JFIF extension APP0 marker segment(s) - see below ]
  240.                 o
  241.                 o
  242.                 o
  243.         X'FF', SOFn, length, frame parameters
  244.            Number of components Nf  = 1 or 3
  245.            1st component    C1    = 1 = Y component
  246.            2nd component    C2    = 2 = Cb component
  247.            3rd component    C3    = 3 = Cr component
  248.                 o
  249.                 o
  250.                 o
  251.     X'FF', EOI
  252.  
  253. JFIF Extension APP0 Marker Segment
  254.  
  255. Immediately following the JFIF APP0 marker segment may be a JFIF extension 
  256. APP0 marker.  This JFIF extension APP0 marker segment may only be present for 
  257. JFIF versions 1.02 and above.  The syntax of the JFIF extension APP0 marker 
  258. segment is:
  259.  
  260.          X'FF', APP0, length, identifier, extension_code, extension_data
  261.             length   (2 bytes)    Total APP0 field byte count, including 
  262.                   the byte count value (2 bytes), but 
  263.                   excluding the APP0 marker itself
  264.             identifier  (5 bytes)    = X'4A', X'46', X'58', X'58', X'00' 
  265.                                   This zero terminated string ("JFXX") 
  266.                   uniquely identifies this APP0 marker.  This 
  267.                   string shall have zero parity (bit 7=0).
  268.             extension_code (1 byte)    = Code which identifies the 
  269.                   extension.  In this version, the following 
  270.                   extensions are defined:
  271.                                    = X'10'   Thumbnail coded using JPEG
  272.                                    = X'11'   Thumbnail stored using 1 byte/
  273.                         pixel
  274.                                    = X'13'   Thumbnail stored using 3 bytes/
  275.                         pixel
  276.             extension_data (variable)    = The specification of the remainder 
  277.                    of the JFIF extension APP0 marker segment 
  278.                   varies with the extension. See below for 
  279.                   a specification of extension_data for each 
  280.                   extension.
  281.  
  282. JFIF Extension:  Thumbnail coded using JPEG
  283.  
  284. This extension supports thumbnails compressed using JPEG.  The compressed 
  285. thumbnail  immediately follows the extension_code (X'10') in the 
  286. extension_data field and the length of the compressed data must be 
  287. included in the JFIF extension APP0 marker length field.
  288.  
  289. The syntax of the extension_data field conforms to the syntax for 
  290. interchange format defined in Annex B of ISO DIS 10918-1.  However, no 
  291. "JFIF" or "JFXX" marker segments shall be present.  As in the full 
  292. resolution image of the JFIF file, the syntax of extension_data constrains 
  293. parameters in the frame header as defined below:
  294.  
  295.     X'FF', SOI
  296.                 o
  297.                 o
  298.                 o
  299.         X'FF', SOFn, length, frame parameters
  300.              Number of components    Nf    = 1 or 3
  301.              1st component    C1    = 1 = Y component
  302.              2nd component    C2    = 2 = Cb component
  303.              3rd component    C3    = 3 = Cr component
  304.                 o
  305.                 o
  306.                 o
  307.     X'FF', EOI
  308.  
  309.  
  310.  
  311. JFIF Extension:  Thumbnail stored using one byte per pixel
  312.  
  313. This extension supports thumbnails stored using one byte per pixel and a 
  314. color palette in the extension_data field.  The syntax of extension_data is:
  315.  
  316.         Xthumbnail       (1 byte)    Thumbnail horizontal pixel count
  317.         Ythumbnail       (1 byte)    Thumbnail vertical pixel count
  318.         palette          (768 bytes) 24-bit RGB pixel values for the color 
  319.                      palette.  The RGB values define the 
  320.                      colors represented by each value of 
  321.                      an 8-bit binary encoding (0 - 255).
  322.         (pixel)n        (n bytes)    8-bit values for the thumbnail pixels 
  323.                                      n = Xthumbnail * Ythumbnail
  324.  
  325. JFIF Extension:  Thumbnail stored using three bytes per pixel
  326.  
  327. This extension supports thumbnails stored using three bytes per pixel in the 
  328. extension_data field.  The syntax of extension_data is:
  329.  
  330.         Xthumbnail       (1 byte)    Thumbnail horizontal pixel count
  331.         Ythumbnail       (1 byte)    Thumbnail vertical pixel count
  332.         (RGB)n           (3n bytes)  Packed (24-bit) RGB values for the 
  333.                      thumbnail pixels, 
  334.                      n = Xthumbnail * Ythumbnail
  335.  
  336. Useful tips
  337.  
  338. o you can identify a JFIF file by looking for the following sequence: 
  339. X'FF', SOI, X'FF', APP0, <2 bytes to be skipped>, "JFIF", X'00'.
  340.  
  341. o if you use APP0 elsewhere, be sure not to have the strings "JFIF" or 
  342. "JFXX" right after the APP0 marker.
  343.  
  344. o if you do not want to include a thumbnail, just program Xthumbnail = 
  345. Ythumbnail = 0.
  346.  
  347. o be sure to check the version number in the special APP0 field.  In 
  348. general, if the major version number of the JFIF file matches that 
  349. supported by the decoder, the file will be decodable.
  350.  
  351. o if you only want to specify a pixel aspect ratio, put 0 for the units 
  352. field in the special APP0 field. Xdensity and Ydensity can then be 
  353. programmed for the desired aspect ratio. Xdensity = 1, Ydensity = 1 
  354. will program a 1:1 aspect ratio.  Xdensity and Ydensity should 
  355. always be non-zero.
  356.  
  357.  
  358.